<?php
/**
 * Created by PhpStorm.
 * User: Gooraye
 * Date: 2016-08-03
 * Time: 10:37
 */

namespace Home\Controller;
use Admin\Api\AngelRoleApi;
use Admin\Api\NewmemberApi;
use Admin\Api\PartnerRoleApi;
use Shop\Api\MadeOrderApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Think\Controller;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersItemApi;
use Weixin\Api\WxuserApi;
class TasktimeController extends Controller{

    public function index(){
        $this->checkinfo();//订单过期重新上架
        $this->confirm_express();//自动确认收货
        $this->reduce_rank();//普通锅会员降级
        $this->angle_down();//天使会员降级
        $this->platina_down();//股东会员降级
        $this->made_order_out_time();//私人定制过期
    }

    public function checkinfo(){
        //过期订单处理将过期订单的商品重新上架
        //找到有未付款的订单
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;

        $distancenow = time() - 60*60*2;
        $ordermap['pay_status'] = 0;
        $ordermap['order_status'] = array('NEQ',8);
        $ordermap['status'] = 1;
        $ordermap['o.createtime'] = array('LT',$distancenow);

        //连表查询找到相应的商品id
        $productidinfo = apiCall(OrdersApi::SELECT_SELL_ORDER,array($ordermap,'o.uid,o.pay_balance,o.pay_score,i.p_id,i.count,i.sku_id'));
        if($productidinfo['status']){
            unset($ordermap['o.createtime']);
            $entity['order_status'] = 8;
            $ordermap['createtime'] = array('LT',$distancenow);
            $orderinfo = apiCall(OrdersApi::SAVE,array($ordermap,$entity));
            if($orderinfo['status']){

                foreach($productidinfo['info'] as $v){
                    //商品重新上架
                    $skumap['product_id'] = $v['p_id'];
                    $skumap['sku_id'] = $v['sku_id'];
                    $skuinfo = apiCall(ProductSkuApi::GET_INFO,array($skumap));
                    if($skuinfo['status']){
                        $skusave['quantity'] = $skuinfo['info']['quantity']+$v['count'];
                        $skures = apiCall(ProductSkuApi::SAVE,array($skumap,$skusave));
                        if(!$skures['status']){
                            $flag = true;
                        }
                    }

                    //将支付的储值回退
                    $money = $v['pay_balance'];
                    if($money > 0){
                        $nmap['wx_id'] = $v['uid'];
                        $ninfo = apiCall(NewmemberApi::GET_INFO,array($nmap));

                        if($ninfo['status']){
                            $lastmoney = $ninfo['info']['store_money'];
                            $store_money = bcadd($money,$lastmoney,2);
                            //获得消费股份累加钱数回退 和消费累计金额回退
                            $total_money = bcsub($ninfo['info']['get_stock_money'],$money,2);
                            $nsave['get_stock_money'] = $total_money;
                            $nsave['spend_money'] = bcsub($ninfo['info']['spend_money'],$money,2);

                            $nsave['store_money'] = $store_money;
                            $nres = apiCall(NewmemberApi::SAVE,array($nmap,$nsave));

                            if(!$nres['status']){
                                $flag = true;
                            }
                        }
                    }

                    //将积分回退
                    $points = $v['pay_score'];
                    if($points > 0){
                        $nmap['wx_id'] = $v['uid'];
                        $ninfo = apiCall(NewmemberApi::GET_INFO,array($nmap));

                        if($ninfo['status']){
                            $lastpoints = $ninfo['info']['points'];
                            $store_money = bcadd($points,$lastpoints,2);
                            $nsave['points'] = $store_money;
                            $nres = apiCall(NewmemberApi::SAVE,array($nmap,$nsave));

                            if(!$nres['status']){
                                $flag = true;
                            }
                        }
                    }

                }
                //还有一种状态没写对于已经卖完的商品重新上架
            }else{
                $flag = true;
            }

        } else{
            $flag = true;
        }

        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-订单重新上架');
            $model->commit();
        }

    }

    //15天后自动确认收货
    public function confirm_express(){
        $model = M();
        $model->startTrans();//开启事务
        $flag = false;
        $ordermap['order_status'] = 4;
        $distancenow = time()-(15*24*3600);
        $ordermap['updatetime'] = array('LT',$distancenow);
        //找到所有该类型的订单
        $ordercode = apiCall(OrdersApi::QUERY_NO_PAGING,array($ordermap,'','order_code'));
        if($ordercode['status']){
            $entity['order_status'] = 5;//表示确认收货交易成功
            $orderinfo = apiCall(OrdersApi::SAVE,array($ordermap,$entity));
            if(!$orderinfo['status']){
                $flag = true;
            }
        }

        if ($flag) {
            $model->rollback();
        } else {
            addWeixinLog('定时器-自动确认收货');
            $model->commit();
        }
    }



    /*
     * 消费会员降级
     * */
    public function reduce_rank(){
        $yeartime = time()-(12*30*24*3600);
        $gmap['role_grade'] = 3;//白金会员
        $gmap['vip_type'] = 1;
        $gmap['spend_money'] = array(
            'LT',
            '5000'
        );
        $gmap['sm_update_time'] = array(
            'LT',
            $yeartime
        );
        $gres = apiCall(NewmemberApi::QUERY_NO_PAGING,array($gmap));

        if($gres['status']){
            if(!empty($gres['info'])){
                $gsarr = array_column($gres['info'],'wx_id');
                $gsmap['wx_id'] = array('in',$gsarr);
                $gsave = array(
                    'role_grade'=>'2',
                    'spend_money'=>'0',
                    's_gold_mumber'=>'1',
                    's_platina_mumber'=>'0',
                );
                $gsres = apiCall(NewmemberApi::SAVE,array($gsmap,$gsave));
            }
        }

        $pmap['role_grade'] = 4;//钻石会员
        $pmap['vip_type'] = 1;
        $pmap['spend_money'] = array(
            'LT',
            '10000'
        );
        $pmap['sm_update_time'] = array(
            'LT',
            $yeartime
        );

        $pres = apiCall(NewmemberApi::QUERY_NO_PAGING,array($pmap));
        if($pres['status']){
            if(!empty($pres['info'])){
                $psarr = array_column($pres['info'],'wx_id');
                if(!empty($pres['info'])){
                    $psmap['wx_id'] = array('in',$psarr);
                    $psave = array(
                        'role_grade'=>'2',
                        'spend_money'=>'0',
                        's_gold_mumber'=>'1',
                        's_platina_mumber'=>'0',
                    );
                    $psres = apiCall(NewmemberApi::SAVE,array($psmap,$psave));
                }
            }

        }

    }


    /*
     * 天使会员降级
     * */
    public function angle_down(){
        $yeartime = time()-365*24*60*60;
        $anglemap['id'] = 1;
        $angle_role = apiCall(AngelRoleApi::GET_INFO,array($anglemap));
        if($angle_role['status']){
            $angle_num = $angle_role['info']['year_recommend'];
            $as_map['role_grade'] = 1;
            $as_map['vip_type'] = 2;
            $as_map['year_recommend_num'] = array('LT',$angle_num);
            $as_map['sm_update_time'] = array(
                'LT',
                $yeartime
            );

            $ares = apiCall(NewmemberApi::QUERY_NO_PAGING,array($as_map));

            if($ares['status']){
                if(!empty($ares['info'])){
                    $asarr = array_column($ares['info'],'wx_id');
                    $asmap['wx_id'] = array('in',$asarr);
                    $asave = array(
                        'role_grade'=>'2',
                        'spend_money'=>'0',
                        's_gold_mumber'=>'1',
                        's_platina_mumber'=>'0',
                    );
                    $asres = apiCall(NewmemberApi::SAVE,array($asmap,$asave));
                }

            }

        }
    }



    /*
     * 股东会员降级
     * */
    public function platina_down(){
        $yeartime = time()-365*24*60*60;
        $partnermap['id'] = 1;
        $partner_role = apiCall(PartnerRoleApi::GET_INFO,array($partnermap));
        if($partner_role['status']){
            $partner_num = $partner_role['info']['year_recommend'];
            $ps_map['role_grade'] = 1;
            $ps_map['vip_type'] = 3;
            $ps_map['year_recommend_num'] = array('LT',$partner_num);
            $ps_map['sm_update_time'] = array(
                'LT',
                $yeartime
            );

            $pres = apiCall(NewmemberApi::QUERY_NO_PAGING,array($ps_map));

            if($pres['status']){
                if(!empty($pres['info'])){
                    $psarr = array_column($pres['info'],'wx_id');
                    $psmap['wx_id'] = array('IN',$psarr);
                    $psave = array(
                        'role_grade'=>'2',
                        'spend_money'=>'0',
                        's_gold_mumber'=>'1',
                        's_platina_mumber'=>'0',
                    );
                    $psres = apiCall(NewmemberApi::SAVE,array($psmap,$psave));
                }

            }

        }
    }

    /*
     * 日历年获得邀请好友的机会
     * 这个定时器 需要在每个日历年的时候执行   已经写入linux定时器了
     **/
    public function calendar_year(){
        $map = array(
            'role_grade'=>'2',
            'vip_type'=> '1',
        );
        $save = array(
            's_gold_mumber'=>'1',
        );
        $res = apiCall(NewmemberApi::SAVE,array($map,$save));

        $map = array(
            'role_grade'=>'3',
            'vip_type'=> '1',
        );
        $save = array(
            's_gold_mumber'=>'2',
        );
        $res = apiCall(NewmemberApi::SAVE,array($map,$save));

        $map = array(
            'role_grade'=>'4',
            'vip_type'=> '1',
        );
        $save = array(
            's_gold_mumber'=>'2',
            's_platina_mumber'=>'1'
        );
        $res = apiCall(NewmemberApi::SAVE,array($map,$save));

        $map = array(
            'role_grade'=>'1',
            'vip_type'=> '2',
        );
        $save = array(
            's_gold_mumber'=>'2',
            's_platina_mumber'=>'1'
        );
        $res = apiCall(NewmemberApi::SAVE,array($map,$save));

        $map = array(
            'role_grade'=>'1',
            'vip_type'=> '3',
        );
        $save = array(
            's_gold_mumber'=>'2',
            's_platina_mumber'=>'1'
        );
        $res = apiCall(NewmemberApi::SAVE,array($map,$save));
    }



    //定时器，私人定制过期
    public function made_order_out_time(){

        $made_order_info=apiCall(MadeOrderApi::QUERY_NO_PAGING,array(['overdue'=>1]));
        if($made_order_info['status']){
            $order_info=$made_order_info['info'];
            foreach($order_info as $k => $v){
                $cur_time=time();
                $order_time=$v['create_time'];
                $distance_time=$cur_time-$order_time;
                if($distance_time>=60*60*24*365*2){

                    $money=$v['money'];
                    $last_money=$v['last_money'];
                    if($last_money>0){
                        //属于有二次充值的情况
                        if($last_money>=$money){
                            //如果最后一次充值金额大于当前金额，说明之前钱包已用完
                            if($v['create_time']<$v['update_time']){
                            //如果创建时间小于更新时间
                            //那么就不过期，更改时间
                            $tran['create_time']=$v['update_time'];
                            $map['uid']=$v['uid'];$map['made_id']=$v['made_id'];
                            $transform=apiCall(MadeOrderApi::SAVE,array($map,$tran));
                            $this->success("更新成功{$v['uid']}");
                            }else{
                                //如果创建时间等于更新时间，说明该过期了
                                $tran['overdue']=0;
                                $map['uid']=$v['uid'];$map['made_id']=$v['made_id'];
                                $transform=apiCall(MadeOrderApi::SAVE,array($map,$tran));
                                $this->success("过期成功{$v['uid']}");
                            }
                        }else{
                            //如果最后一次充值金额小于当前金额，说明之前的钱包未用完
                            //余额调整为最后充值余额，并更新时间
                            $tran['create_time']=$v['update_time'];
                            $tran['money']=$last_money;
                            $map['uid']=$v['uid'];$map['made_id']=$v['made_id'];
                            $transform=apiCall(MadeOrderApi::SAVE,array($map,$tran));
                            $this->success("过期并更新成功{$v['uid']}");
                        }

                    }else{
                        //只充值了一次的情况
                        $tran['overdue']=0;
                        $map['uid']=$v['uid'];$map['made_id']=$v['made_id'];
                        $transform=apiCall(MadeOrderApi::SAVE,array($map,$tran));
                        $this->success("过期成功{$v['uid']}");
                    }

                }
            }
        }

     }


}